<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:math="http://exslt.org/math"
  xmlns:umbraco.library="urn:umbraco.library"
  xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes"
  xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
  xmlns:BlogLibrary="urn:BlogLibrary"
  exclude-result-prefixes="msxml math umbraco.library Exslt.ExsltDatesAndTimes Exslt.ExsltSets BlogLibrary ">
  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:param name="currentPage"/>
  <xsl:key name="year" match="*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id)]" use="Exslt.ExsltDatesAndTimes:year(./postDate)" />
  <xsl:key name="month" match="*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id)]" use="Exslt.ExsltDatesAndTimes:monthinyear(./postDate)" />
  <xsl:variable name="blog" select="$currentPage/ancestor-or-self::*[(@isDoc or name()='node') and BlogLibrary:IsType('Blog', @id)]" />
  <xsl:template match="/">
    <div id="archive">
      <xsl:for-each select="$blog//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id) and generate-id()=generate-id(key('year',Exslt.ExsltDatesAndTimes:year(./postDate)))]">
        <xsl:sort select="./postDate" data-type="number" order="descending" />
        <xsl:variable name="year" select="Exslt.ExsltDatesAndTimes:year(./postDate)" />
        
        <h3><xsl:value-of select="$year" disable-output-escaping="yes" /></h3>
        
        <xsl:for-each select="$blog//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id) and generate-id()=generate-id(key('month',Exslt.ExsltDatesAndTimes:monthinyear(./postDate)))]">
          <xsl:sort select="Exslt.ExsltDatesAndTimes:monthinyear(./postDate)" data-type="number" order="descending" />
          <xsl:variable name="month" select="Exslt.ExsltDatesAndTimes:monthinyear(./postDate)" />
          
          <h4><xsl:value-of select="Exslt.ExsltDatesAndTimes:monthname(./postDate)"/></h4>
          <div class="tab">
            <ul>
              <xsl:for-each select="$blog//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id) and
              Exslt.ExsltDatesAndTimes:year(./postDate) = $year and
              Exslt.ExsltDatesAndTimes:monthinyear(./postDate) = $month]">
                <xsl:sort select="Exslt.ExsltDatesAndTimes:dayinmonth(./postDate)" data-type="number" order="descending" />
                <li>
                  <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a><br/>
                  <small>Posted: <xsl:value-of select="umbraco.library:LongDate(./postDate)"/> by: <xsl:value-of select="@writerName"/></small>
                </li>
              </xsl:for-each>
            </ul>
          </div>
        </xsl:for-each>
      </xsl:for-each>
    </div>
  </xsl:template>
</xsl:stylesheet>